package com.rzhd.coursepatriarch.common.view.dialog;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rzhd.coursepatriarch.common.R;
import com.rzhd.coursepatriarch.common.utils.CommonUtil;

/**
 * SimpleDialog
 * 改为继承自DialogFragment
 * 因之前的Dialog会持有Activity实例，
 * 如果Activity销毁前未关闭Dialog会导致Activity实例无法被回收，从而造成内存泄露问题
 * 而DialogFragment的生命周期依赖Activity的生命周期，
 * DialogFragment在销毁时
 * @author LuDaiqian
 * @author ymh
 *
 */
public class CusstomLoadingDialog extends DialogFragment {

	protected static final String TAG = "IphoneDialog";
	private OnDialogCancelListener cancelListener;
	private boolean mCancel;
	private boolean isSimple;           //是否使用简单版本

	//====================2018-11-13新增===============
	public static CusstomLoadingDialog newInstance(boolean isSimple ) {
		CusstomLoadingDialog loadingDialog = new CusstomLoadingDialog();
		loadingDialog.setSimple( isSimple );
		loadingDialog.setStyle(STYLE_NO_TITLE, 0);
		return loadingDialog;
	}
	//====================2018-11-13新增===============

	public CusstomLoadingDialog(){
	}

	@Override
	public void onStart() {
		super.onStart();
		//============================有时getDialog()会返回空==================
		try{
			Window window = getDialog().getWindow();
			WindowManager.LayoutParams windowParams = window.getAttributes();
			windowParams.dimAmount = 0.0f;
			window.setAttributes(windowParams);
		}catch ( Exception e ){
			e.printStackTrace();
		}
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		getDialog().setCanceledOnTouchOutside(mCancel);
//		R.layout.progress_bar-------------2018-5-6修改
		View contentView = inflater.inflate( R.layout.cusstom_progress_bar_simple_layout, container, false);
		//====================2018-11-13新增=================
		RelativeLayout containerLayout = contentView.findViewById( R.id.cusstom_progress_bar_dialog_container );
		TextView hitText = contentView.findViewById( R.id.cusstom_progress_bar_dialog_hit_text );
		if( isSimple ){
			containerLayout.setLayoutParams(
					new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
			hitText.setVisibility( View.GONE );
		}else{
			containerLayout.setLayoutParams(
					new LinearLayout.LayoutParams(CommonUtil.dip2px( getContext() , 240 ),
							CommonUtil.dip2px( getContext() , 150 )));
			hitText.setText( getText( R.string.handling_text ));
			hitText.setVisibility( View.VISIBLE );
		}
		//====================2018-11-13新增=================
		initView(contentView);
		return contentView;
	}

	private void initView(View contentView) {
		//2018-5-6修改
//		View loadingIcon = contentView.findViewById(R.id.progressbar);
		View loadingIcon = contentView.findViewById(R.id.cusstom_progressbar);
		Animation roteAnimation = new RotateAnimation(0.0f, 360f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		roteAnimation.setRepeatCount(-1);
		roteAnimation.setInterpolator(new LinearInterpolator());
		roteAnimation.setDuration(2000);

		loadingIcon.startAnimation(roteAnimation);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		super.onCancel(dialog);
		if(cancelListener != null){
			cancelListener.onCancel();
		}
	}

	public void setCancelOutSideTouched(boolean cancel){
		mCancel = cancel;
	}

	@Override
	public void show(FragmentManager manager, String tag) {
		super.show(manager, tag);
	}

	public void show(FragmentManager manager) {
		super.show(manager, "loading");
	}

	public void setOnDialogCancelListener(OnDialogCancelListener cancelListener){
		this.cancelListener = cancelListener;
	}
	public interface OnDialogCancelListener{
		void onCancel();
	}

	public void setSimple(boolean simple) {
		isSimple = simple;
	}
}
